package fr.imag.ufrima.ima.project.ui.tools;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.log4j.Logger;

import fr.imag.ufrima.ima.project.core.Filter;
import fr.imag.ufrima.ima.project.core.operations.FilterOperations;
import fr.imag.ufrima.ima.project.ui.main.ImageProject;

@SuppressWarnings("serial")
public class FlouTool extends JPanel {	
    private Logger logger = Logger.getLogger(getClass().getName());
    	
	private ImageProject imageProject;
	private JLabel valeur;

	/**
	 * Create the panel.
	 */
	public FlouTool(ImageProject imageProject) {
		this.imageProject = imageProject;
		
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{180};
		gbl_panel.rowHeights = new int[]{0, 0, 0,0,0,0};
		gbl_panel.columnWeights = new double[]{0.0};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JLabel lblSelectionnerLoutil = new JLabel("Choisir l'intensité du flou:");
		GridBagConstraints gbc_lblSelectionnerLoutil = new GridBagConstraints();
		gbc_lblSelectionnerLoutil.insets = new Insets(10, 0, 5, 0);
		gbc_lblSelectionnerLoutil.gridx = 0;
		gbc_lblSelectionnerLoutil.gridy = 0;
		panel.add(lblSelectionnerLoutil, gbc_lblSelectionnerLoutil);
		
		JSlider flou = new JSlider(1, 19, 1);
		GridBagConstraints gbc_btnFlou = new GridBagConstraints();
		gbc_btnFlou.fill = GridBagConstraints.BOTH;
		gbc_btnFlou.insets = new Insets(0, 0, 5, 0);
		gbc_btnFlou.gridx = 0;
		gbc_btnFlou.gridy = 1;
		flou.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int value;
				JSlider source = (JSlider)e.getSource();
				value = source.getValue();
				if(value%2 == 0)
					value ++;
				valeur.setText(value+"");
			}
		});
		panel.add(flou, gbc_btnFlou);
		
		valeur = new JLabel("1");
		gbc_btnFlou = new GridBagConstraints();
		gbc_btnFlou.insets = new Insets(0, 0, 5, 0);
		gbc_btnFlou.gridx = 0;
		gbc_btnFlou.gridy = 3;
		panel.add(valeur, gbc_btnFlou);
		
		JButton appliquer = new JButton("Appliquer");
		gbc_btnFlou = new GridBagConstraints();
		gbc_btnFlou.fill = GridBagConstraints.BOTH;
		gbc_btnFlou.insets = new Insets(0, 0, 5, 0);
		gbc_btnFlou.gridx = 0;
		gbc_btnFlou.gridy = 5;
		appliquer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				FlouTool.this.applyFlou();
			}
		});
		panel.add(appliquer, gbc_btnFlou);
	}
	
	private void applyFlou() {
		logger.info(String.format("About to executing flou ..."));
		
		Filter filter = new Filter(Integer.parseInt(valeur.getText()));						
		FilterOperations filterOperations = new FilterOperations(imageProject.getCurrentImageDocument().getImage());
		filterOperations.setFilter(filter);
		
		imageProject.applyOperation(filterOperations);	
	}
}
